package com.example.administrator.hookdemo;


import android.app.Activity;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2020/6/14.
 */

public class InstrumentationProxy extends Instrumentation {

    Instrumentation mInstrumentation;

    public InstrumentationProxy(Instrumentation instrumentation){
        mInstrumentation = instrumentation;
    }

    @Override
    public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options) {

        Log.e("androidLog","hook成功 --who:"+who);
        try {
            Method execStartActivityMethod = Instrumentation.class.getDeclaredMethod("execStartActivity",Context.class,IBinder.class,IBinder.class,Activity.class,Intent.class,int.class,Bundle.class);
            return (ActivityResult) execStartActivityMethod.invoke(mInstrumentation,who,contextThread,token,target,intent,requestCode,options);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}
